package com.corewillsoft.fireflies.gamelaws;

import android.util.FloatMath;

import com.corewillsoft.fireflies.gameobjects.Firefly;
import com.corewillsoft.fireflies.gameobjects.GameObject;

public class FingerForceLaw extends BaseLaw {

	private final float LAW_FORCE = 1f; // force of law
	private final float INFLUENCE_RADIUS = 200f; // radius of area where the law
	// acts on fireflies
	private final float CHARGE_RATE = 0.005f; // how fast the law charges
	// fireflies
	private float fingerX;
	private float fingerY;

	public FingerForceLaw() {
	}

	public FingerForceLaw(float fingerX, float fingerY) {
		this.fingerX = fingerX;
		this.fingerY = fingerY;
	}

	public void setFingerX(float fingerX) {
		this.fingerX = fingerX;
	}

	public void setFingerY(float fingerY) {
		this.fingerY = fingerY;
	}

	@Override
	public void applicate(GameObject gameObject) {
		if (gameObject instanceof Firefly)
			processFirefly((Firefly) gameObject);
	}

	private void processFirefly(Firefly gameObject) {
		Firefly object = gameObject;
		int objectX = (int) object.getX();
		int objectY = (int) object.getY();

		float dX = fingerX - objectX;
		float dY = fingerY - objectY;
		float length = FloatMath.sqrt(dX * dX + dY * dY);

		if (INFLUENCE_RADIUS < length)
			return;

		float offsetX = 0;
		float offsetY = 0;

		if (0 < length) {
			offsetX = LAW_FORCE * dX / length;
			offsetY = LAW_FORCE * dY / length;
		}

		object.setSpeed(object.getSpeedX() + offsetX, object.getSpeedY()
				+ offsetY);
		object.setEnergy(object.getEnergy() + CHARGE_RATE);
	}

	@Override
	public LawType getType() {
		return LawType.FINGER_FORCE;
	}
}